<?php

namespace app\common\model\Hotel;

use think\Model;
// 引入软删除的模型
use traits\model\SoftDelete;

class Order extends Model
{
    //在模型内部去引入软删除
    use SoftDelete;
    //
    protected $name = "hotel_order";

    //指定一个自动设置的时间字段
    //开启自动写入
    protected $autoWriteTimestamp = true;

    //设置字段的名字
    protected $createTime = "createtime"; //插入的时候设置的字段名

    //禁止 写入的时间字段
    protected $updateTime = false;


    //设置软删除的字段更新的时间字段
    protected $deleteTime = 'deletetime';

    protected $append = [
        'createtime_text',
        'create_text',
        'starttime_text',
        'endtime_text',
        'status_text',
        'startweek_text',
        'endweek_text',
        'day_text',
        'start_text',
        'end_text',
        'guest_text'
    ];


    public function getCreateTimeTextAttr($value,$data){
        $createtime = isset($data['createtime']) ? trim($data['createtime']) : '';
        if(empty($createtime)){
            return '';
        }
        return date('Y-m-d H:i',$createtime);
    }

    public function getCreateTextAttr($value,$data){
        $createtime = isset($data['createtime']) ? trim($data['createtime']) : '';
        if(empty($createtime)){
            return '';
        }
        $str = date('Y-m-d-H-i-s',$createtime);
        $arr = explode('-', $str);
        $word = "$arr[0]年$arr[1]月$arr[2]日 $arr[3]:$arr[4]:$arr[5]";
        return $word;
    }

    public function getStartTimeTextAttr($value,$data){
        $starttime = isset($data['starttime']) ? trim($data['starttime']) : '';
        if(empty($starttime)){
            return '';
        }
        return date('Y-m-d',$starttime);
    }

    public function getStartTextAttr($value,$data){
        $starttime = isset($data['starttime']) ? trim($data['starttime']) : '';
        if(empty($starttime)){
            return '';
        }
        $str = date('Y-m-d',$starttime);
        $arr = explode('-', $str);
        $word = "$arr[0]年$arr[1]月$arr[2]日";
        return $word;
    }

    public function getEndTimeTextAttr($value,$data){
        $endtime = isset($data['endtime']) ? trim($data['endtime']) : '';
        if(empty($endtime)){
            return '';
        }
        return date('Y-m-d',$endtime);
    }

    public function getEndTextAttr($value,$data){
        $endtime = isset($data['endtime']) ? trim($data['endtime']) : '';
        if(empty($endtime)){
            return '';
        }
        $str = date('Y-m-d',$endtime);
        $arr = explode('-', $str);
        $word = "$arr[0]年$arr[1]月$arr[2]日";
        return $word;
    }

    public function getStatusTextAttr($value,$data){
        $status = isset($data['status']) ? trim($data['status']) : '';

        $status_list = [-1 => '售后中', -2 => '退款成功', -3 => '退款失败', 1 => '已支付', 2 => '已入住', 3 => '已退房', 4 => '已评价'];

        if($status >= -3){
            return $status_list[$status];
        }
    }

    public function getStartWeekTextAttr($value,$data){
        $week_array = array("日","一","二","三","四","五","六");
        $starttime = isset($data['starttime']) ? trim($data['starttime']) : '';
        if(empty($starttime)){
            return '';
        }

        $week = date('w',$starttime);

        return "星期".$week_array[$week];
    }

    public function getEndWeekTextAttr($value,$data){
        $week_array = array("日","一","二","三","四","五","六");
        $endtime = isset($data['endtime']) ? trim($data['endtime']) : '';
        if(empty($endtime)){
            return '';
        }
        $week = date('w',$endtime);

        return "星期".$week_array[$week];
    }

    public function getDayTextAttr($value,$data){
        $starttime = isset($data['starttime']) ? trim($data['starttime']) : '';
        $endtime = isset($data['endtime']) ? trim($data['endtime']) : '';
        $day = intval(($endtime - $starttime) / 86400);

        return $day;
    }

    // 入住人
    public function getGuestTextAttr($value,$data){
        $guest = isset($data['guest']) ? trim($data['guest']) : 0;
        if(empty($guest)){
            return '';
        }
        $arr = explode(',',$guest);
        $guest = model('Hotel.Guest')->where(['id' => ['IN',$arr]])->column('nickname');
        if(!$guest){
            return '';
        }
        $str = implode(',',$guest);
        return $str;
        
    }

    // 关联查询房间
    public function room(){
        return $this->belongsTo('app\common\model\Hotel\Room','roomid','id',[],'LEFT')->setEagerlyType(0);
    }

    // 关联查询用户
    public function business(){
        return $this->belongsTo('app\common\model\Business\User','busid','id',[],'LEFT')->setEagerlyType(0);
    }

    // 关联查询优惠券
    public function coupon(){
        return $this->belongsTo('app\common\model\Hotel\Coupon','cid','id',[],'LEFT')->setEagerlyType(0);
    }

}
